package org.dromara.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.dromara.app.appParams.AppReportParams;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.SkReportBo;
import org.dromara.app.domain.vo.SkReportVo;
import org.dromara.app.domain.SkReport;
import org.dromara.app.mapper.SkReportMapper;
import org.dromara.app.service.ISkReportService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 举报信息Service业务层处理
 *
 * @author Evacuation
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
public class SkReportServiceImpl implements ISkReportService {

    private final SkReportMapper baseMapper;

    /**
     * 查询举报信息
     *
     * @param id 主键
     * @return 举报信息
     */
    @Override
    public SkReportVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询举报信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 举报信息分页列表
     */
    @Override
    public TableDataInfo<SkReportVo> queryPageList(SkReportBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<SkReport> lqw = buildQueryWrapper(bo);
        //判断是否城市代理账户
        LoginUser user = LoginHelper.getLoginUser();
        if (user.getRolePermission().contains("cityAgent")){
            //是城市代理人
            bo.setCity(user.getCity());
        }
        Page<SkReportVo> result = baseMapper.selectNewVoPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的举报信息列表
     *
     * @param bo 查询条件
     * @return 举报信息列表
     */
    @Override
    public List<SkReportVo> queryList(SkReportBo bo) {
        LambdaQueryWrapper<SkReport> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkReport> buildQueryWrapper(SkReportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkReport> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SkReport::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SkReport::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), SkReport::getImage, bo.getImage());
        return lqw;
    }

    /**
     * 新增举报信息
     *
     * @param bo 举报信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SkReportBo bo) {
        SkReport add = MapstructUtils.convert(bo, SkReport.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改举报信息
     *
     * @param bo 举报信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SkReportBo bo) {
        SkReport update = MapstructUtils.convert(bo, SkReport.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkReport entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除举报信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


    /**
     * 举报
     * @param userId
     * @param params
     */
    @Override
    public void putReport(Long userId, AppReportParams params) {
        SkReport skReport = BeanUtil.copyProperties(params,SkReport.class);
        skReport.setUserId(userId);
        baseMapper.insert(skReport);
    }
}
